﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;


namespace OF.Component.DAL
{
    ///<summary>
    ///VerificationCodeMessage 数据访问层For SqlServer 
    ///</summary>
    internal partial class VerificationCodeMessage : IDAL.IVerificationCodeMessage
    {
        #region 根据手机号与验证码类型取出记录
        ///<summary>
        ///根据手机号与验证码类型取出记录
        ///</summary>
        public VerificationCodeMessageInfo GetByTypeAndMobile(string mobile, int typeId, string checkCode)
        {
            string strSql = @"SELECT * FROM dbo.VerificationCodeMessage WHERE Mobile=@Mobile AND TypeId=@TypeId AND CheckCode=@CheckCode AND ExpirationTime>GETDATE()";
            Model.VerificationCodeMessageInfo info = null;
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddInParameter(dbc, "@Mobile", DbType.String, mobile);
            db.AddInParameter(dbc, "@TypeId", DbType.Int32, typeId);
            db.AddInParameter(dbc, "@CheckCode", DbType.String, checkCode);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }
        #endregion

        ///<summary>
        ///根据ID取出记录
        ///</summary>
        public VerificationCodeMessageInfo GetByMobileAndTypeId(int typeId, string mobile)
        {
            Model.VerificationCodeMessageInfo info = null;

            string strSql = @"SELECT * FROM dbo.VerificationCodeMessage WHERE Mobile=@Mobile AND TypeId=@TypeId AND ExpirationTime>GETDATE()";

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddInParameter(dbc, "@Mobile", DbType.String, mobile);
            db.AddInParameter(dbc, "@TypeId", DbType.Int32, typeId);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }

    }
}
